@echo off
setlocal enabledelayedexpansion

REM 跨平台构建脚本 (Windows版本)
REM 支持Android和iOS的自动化构建

echo 🚀 开始跨平台构建...

REM 颜色定义 (Windows CMD不支持ANSI颜色，使用文本标识)
set "INFO=[INFO]"
set "WARNING=[WARNING]"
set "ERROR=[ERROR]"
set "STEP=[STEP]"

REM 打印消息函数
:print_message
echo %INFO% %~1
goto :eof

:print_warning
echo %WARNING% %~1
goto :eof

:print_error
echo %ERROR% %~1
goto :eof

:print_step
echo %STEP% %~1
goto :eof

REM 检查Flutter环境
:check_flutter
call :print_step "检查Flutter环境..."
flutter --version >nul 2>&1
if errorlevel 1 (
    call :print_error "Flutter未安装或未添加到PATH"
    exit /b 1
)

flutter doctor
call :print_message "Flutter环境检查完成"
goto :eof

REM 清理构建缓存
:clean_build
call :print_step "清理构建缓存..."
flutter clean
flutter pub get
call :print_message "构建缓存清理完成"
goto :eof

REM 构建Android APK
:build_android_apk
call :print_step "构建Android APK..."

REM 检查Android环境
flutter doctor | findstr "Android toolchain" >nul
if errorlevel 1 (
    call :print_warning "Android工具链未配置，跳过Android构建"
    goto :eof
)

REM 构建调试版本
flutter build apk --debug
call :print_message "Android调试APK构建完成"

REM 构建发布版本
flutter build apk --release
call :print_message "Android发布APK构建完成"

REM 构建AAB (Google Play)
flutter build appbundle --release
call :print_message "Android App Bundle构建完成"
goto :eof

REM 构建Android (原生)
:build_android_native
call :print_step "构建原生Android应用..."

if exist "app\build.gradle.kts" (
    REM 使用Gradle构建
    call gradlew clean
    call gradlew assembleDebug
    call gradlew assembleRelease
    call gradlew bundleRelease
    call :print_message "原生Android构建完成"
) else (
    call :print_warning "未找到原生Android项目文件"
)
goto :eof

REM 构建iOS (仅显示信息，实际构建需要macOS)
:build_ios
call :print_step "构建iOS应用..."

REM 检查iOS环境
flutter doctor | findstr "iOS toolchain" >nul
if errorlevel 1 (
    call :print_warning "iOS工具链未配置，跳过iOS构建"
    goto :eof
)

call :print_warning "iOS构建需要在macOS上进行"
call :print_message "请使用macOS系统进行iOS构建"
goto :eof

REM 运行测试
:run_tests
call :print_step "运行测试..."
flutter test
call :print_message "测试完成"
goto :eof

REM 分析代码
:analyze_code
call :print_step "分析代码..."
flutter analyze
call :print_message "代码分析完成"
goto :eof

REM 显示构建结果
:show_build_results
call :print_step "构建结果:"

REM Android APK
if exist "build\app\outputs\flutter-apk\app-release.apk" (
    call :print_message "✅ Android APK: build\app\outputs\flutter-apk\app-release.apk"
)

REM Android AAB
if exist "build\app\outputs\bundle\release\app-release.aab" (
    call :print_message "✅ Android AAB: build\app\outputs\bundle\release\app-release.aab"
)

REM 原生Android APK
if exist "app\build\outputs\apk\release\app-release.apk" (
    call :print_message "✅ 原生Android APK: app\build\outputs\apk\release\app-release.apk"
)

REM iOS (仅显示信息)
call :print_message "ℹ️ iOS构建需要在macOS上进行"
goto :eof

REM 主函数
:main
call :print_message "开始跨平台构建流程..."

REM 检查Flutter环境
call :check_flutter
if errorlevel 1 goto :eof

REM 清理构建缓存
call :clean_build

REM 分析代码
call :analyze_code

REM 运行测试
call :run_tests

REM 构建Flutter应用
call :build_android_apk
call :build_ios

REM 构建原生Android应用
call :build_android_native

REM 显示构建结果
call :show_build_results

call :print_message "🎉 跨平台构建完成！"
goto :eof

REM 处理命令行参数
if "%1"=="android" (
    call :check_flutter
    if errorlevel 1 goto :eof
    call :clean_build
    call :build_android_apk
    call :build_android_native
    goto :end
)

if "%1"=="ios" (
    call :check_flutter
    if errorlevel 1 goto :eof
    call :clean_build
    call :build_ios
    goto :end
)

if "%1"=="test" (
    call :check_flutter
    if errorlevel 1 goto :eof
    call :run_tests
    goto :end
)

if "%1"=="analyze" (
    call :check_flutter
    if errorlevel 1 goto :eof
    call :analyze_code
    goto :end
)

if "%1"=="clean" (
    call :clean_build
    goto :end
)

REM 默认执行全部构建
call :main

:end
echo.
echo 构建脚本执行完成！
pause 